package com.young.commom.httpclient.components;

import com.young.commom.httpclient.HttpClientConfiguration;
import com.young.common.expression.dns.NamingResolver;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 1:06 PM
 */
public final class HttpComponentsClientBuilder {
    public HttpComponentsClientBuilder() {
    }

    public static CloseableHttpClient build(HttpClientConfiguration configuration) {
        return build(configuration, (List)null, (List)null);
    }

    public static CloseableHttpClient build(HttpClientConfiguration configuration, List<HttpRequestInterceptor> requestInterceptors, List<HttpResponseInterceptor> responseInterceptors) {
        HttpClientBuilder builder = HttpClientBuilder.create().disableCookieManagement();
        DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(configuration.getRetryTimes(), configuration.getRetryOnFailure());
        builder.setRetryHandler(retryHandler);
        builder.setConnectionTimeToLive(configuration.getKeepAliveTime(), TimeUnit.MILLISECONDS);
        Iterator var5;
        if (requestInterceptors != null) {
            var5 = requestInterceptors.iterator();

            while(var5.hasNext()) {
                HttpRequestInterceptor interceptor = (HttpRequestInterceptor)var5.next();
                builder.addInterceptorLast(interceptor);
            }
        }

        if (responseInterceptors != null) {
            var5 = responseInterceptors.iterator();

            while(var5.hasNext()) {
                HttpResponseInterceptor interceptor = (HttpResponseInterceptor)var5.next();
                builder.addInterceptorLast(interceptor);
            }
        }

        RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(configuration.getConnectionTimeout().intValue()).setSocketTimeout(configuration.getSocketTimeout().intValue()).setRedirectsEnabled(true).setConnectionRequestTimeout(configuration.getRequestTimeout().intValue()).build();
        builder.setDefaultRequestConfig(defaultRequestConfig);
        builder.setMaxConnTotal(configuration.getMaxConnections());
        builder.setMaxConnPerRoute(128);
        builder.evictExpiredConnections();
        builder.evictIdleConnections(6L, TimeUnit.SECONDS);
        if (configuration.isDnsResolverEnabled()) {
            NamingResolver namingResolver = configuration.getNamingResolver();
            if (namingResolver != null) {
                builder.setDnsResolver(namingResolver::resolve);
            }
        }

        return builder.build();
    }

    public static CloseableHttpClient build() {
        HttpClientConfiguration configuration = HttpClientConfiguration.common();
        return build(configuration);
    }
}
